package cn.ito.zjgsu.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.ito.zjgsu.domain.Authority;
import cn.ito.zjgsu.domain.AuthorityExample;
import cn.ito.zjgsu.domain.RoleAuthority;
import cn.ito.zjgsu.domain.RoleAuthorityExample;
import cn.ito.zjgsu.mapping.AuthorityMapper;
import cn.ito.zjgsu.mapping.RoleAuthorityMapper;
import cn.ito.zjgsu.mapping.RoleMapper;
import cn.ito.zjgsu.service.RolePerMissionService;

@Service
@Transactional
public class PerMissionServiceImpl implements RolePerMissionService{

	@Resource
	private RoleMapper roleMapper;
	
	@Resource
	private AuthorityMapper authorityMapper;
	
	@Resource
	private RoleAuthorityMapper roleAuthorityMapper;
	
	
	@Override
	public List<String> findPermissionsByRoleid(String rid) {
		List<String> permissionsIds = new ArrayList<String>();
		RoleAuthorityExample roleAuthorityExample = new RoleAuthorityExample();
		roleAuthorityExample.or().andRoleIdEqualTo(rid);
		List<RoleAuthority> roleAuthoritys = roleAuthorityMapper.selectByExample(roleAuthorityExample);
		if(null != roleAuthoritys){
			for(RoleAuthority ra : roleAuthoritys){
				permissionsIds.add(ra.getAuthorityId());
			}
		}
		if(null != permissionsIds && !permissionsIds.isEmpty()){
			AuthorityExample authorityExample = new AuthorityExample();
			authorityExample.or().andAuthorityIdIn(permissionsIds);
			List<Authority> authorities = authorityMapper.selectByExample(authorityExample);
			List<String> permissions = new ArrayList<String>();
			if(null != authorities)
			for(Authority a : authorities){
				permissions.add(a.getAuthorityName());
			}
			return permissions;
		}
		
		return null;
	}

}
